﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace Gladkikh.Sims2.Combiner.Controls
{
    using Database;
    public partial class AddonControl : UserControl
    {
        public AddonControl()
        {
            InitializeComponent();
            Addon = new Sims2Addon();
        }

        public AddonControl(Sims2Addon s2d)
        {
            InitializeComponent();
            Addon = s2d;
            Animation = true;
        }

        Sims2Addon addon;

        public Sims2Addon Addon
        {
            get
            {
                return addon;
            }
            set
            {
                addon = value;
                if(value !=null)
                {
                checkBox1.Checked = value.Enabled;
                textBox1.Text = value.ExePath;
                checkBox1.Text = value.Name;
                }
                else
                {
                	textBox1.Text = "";
                	checkBox1.Text = "";
                }
            }
        }

        static public int maxSize;

        public void SetSize(int size)
        {
            Size = new Size(size, Size.Height);
        }

        private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (File.Exists(textBox1.Text))
                openFileDialog1.InitialDirectory = Path.GetDirectoryName(textBox1.Text);

            openFileDialog1.Filter = addon.Name + "(" + addon.Exe + ")|" + addon.Exe;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            addon.Enabled = checkBox1.Checked;
        }

        public void UpdateIt()
        {
            textBox1.Text = addon.ExePath;
            checkBox1.Text = addon.Name;
        }

        private static bool isGameFolder(string path)
        {
            // return true;
            string p = Path.GetDirectoryName(path);
            p = Path.GetDirectoryName(p);
            string[] dirs = Directory.GetDirectories(p, "??Bin");

            if (dirs.Length > 0)
                return true;
            else
                return false;
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (File.Exists(textBox1.Text))
            {
                if (isGameFolder(textBox1.Text))
                {
                    addon.ExePath = textBox1.Text;
                    checkBox1.Enabled = true;
                }
                else
                {
                    checkBox1.Enabled = false;
                }
            }
        }


        public void SetCheckBoxSize(int size)
        {
            tableLayoutPanel1.ColumnStyles[0].SizeType = SizeType.Absolute;
            tableLayoutPanel1.ColumnStyles[0].Width = size + 5;
        }

        public int CheckBoxSize
        {
            get { return checkBox1.Width; }
        }

        private void редактроватьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editor ed = new Editor(addon);

            if(ed.ShowDialog()== DialogResult.OK)
            {
                addon.Name = ed.addon.Name;
                addon.Exe = ed.addon.Exe;
                addon.RegMask = ed.addon.RegMask;

                UpdateIt();
            }
            
        }

        private bool animation;


        public bool Animation
        {
            get { return animation; }
            set { animation = value; }
        }
        
        private void AddonControl_MouseEnter(object sender, EventArgs e)
        {
            if (Animation)
            {
                if (BorderStyle != BorderStyle.FixedSingle)
                    BorderStyle = BorderStyle.FixedSingle;
            }
        }

        private void AddonControl_MouseLeave(object sender, EventArgs e)
        {
            if (Animation)
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
                BorderStyle = BorderStyle.None;
                timer1.Enabled = false;
        }
    }
}
